package com.maimang.platform.crm.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRoleGetroleRequest;
import com.dingtalk.api.request.OapiRoleGetrolegroupRequest;
import com.dingtalk.api.request.OapiRoleListRequest;
import com.dingtalk.api.request.OapiRoleSimplelistRequest;
import com.dingtalk.api.response.OapiRoleGetroleResponse;
import com.dingtalk.api.response.OapiRoleGetrolegroupResponse;
import com.dingtalk.api.response.OapiRoleListResponse;
import com.dingtalk.api.response.OapiRoleSimplelistResponse;
import com.maimang.platform.crm.config.URLConstant;
import com.maimang.platform.crm.service.DingTalkRoleService;
import com.maimang.platform.crm.util.AccessTokenUtil;
import org.springframework.stereotype.Service;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DingTalkRoleServiceImpl
 * @description <br>钉钉角色管理服务层实现</br>
 * @date 2019-09-22 14:18
 * @email jintaozhao@qq.com
 */
@Service("dingTalkRoleService")
public class DingTalkRoleServiceImpl implements DingTalkRoleService {
	/**
	 * 获取角色组
	 *
	 * @param groupId 角色组的Id
	 * @return OapiRoleGetrolegroupResponse
	 * @throws Exception
	 */
	@Override
	public OapiRoleGetrolegroupResponse getRoleGroup(Long groupId) throws Exception {
		String accessToken = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.GET_ROLE_GROUP);
		OapiRoleGetrolegroupRequest request = new OapiRoleGetrolegroupRequest();
		request.setGroupId(groupId);

		OapiRoleGetrolegroupResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取角色列表
	 *
	 * @param offset 分页偏移，默认值：0
	 * @param size   分页大小，默认值：20，最大值200
	 * @return OapiRoleListResponse
	 * @throws Exception
	 */
	@Override
	public OapiRoleListResponse getRoleList(Long offset, Long size) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.ROLE_LIST);
		String accessToken = AccessTokenUtil.getToken();
		OapiRoleListRequest request = new OapiRoleListRequest();
		request.setOffset(offset);
		request.setSize(size);

		OapiRoleListResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取角色下的员工列表
	 *
	 * @param roleId 角色ID
	 * @param offset 分页偏移，默认值：0
	 * @param size   分页大小，默认值：20，最大值200
	 * @return OapiRoleSimplelistResponse
	 * @throws Exception
	 */
	@Override
	public OapiRoleSimplelistResponse getSimpleList(Long roleId, Long offset, Long size) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.ROLE_SIMPLELIST);
		String accessToken = AccessTokenUtil.getToken();
		OapiRoleSimplelistRequest request = new OapiRoleSimplelistRequest();
		request.setRoleId(roleId);
		request.setOffset(offset);
		request.setSize(size);

		OapiRoleSimplelistResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取角色详情
	 *
	 * @param roleId 角色Id
	 * @return OapiRoleGetroleResponse
	 * @throws Exception
	 */
	@Override
	public OapiRoleGetroleResponse getRole(Long roleId) throws Exception {
		String accessToken = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.ROLE_GETROLE);
		OapiRoleGetroleRequest request = new OapiRoleGetroleRequest();
		request.setRoleId(roleId);
		OapiRoleGetroleResponse response = client.execute(request, accessToken);
		return response;
	}
}
